package com.vm.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.vm.commons.VMConst;
import com.vm.model.UserAddress;
import com.vm.utils.CityDialog;
import com.vm.utils.MyVolley;
import com.vm.utils.CityDialog.Dialogcallback;
import com.wwtscapp.apk.R;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月18日 下午1:38:32]
 * @Version: [v1.0]
 * 
 */
public class AddressEditActivity extends Activity {

	RequestQueue rQueue;
	EditText et_address;
	EditText et_name;
	EditText et_tel;
	EditText et_code;
	Button btn_save;
	UserAddress userAddress;
	ImageView imgbtn_search_back;
	int addressId;
	Button btn_delete;
	int order;
	String code;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.address_edit);
		order = getIntent().getIntExtra("order", 0);
		init();

		Object object = getIntent().getSerializableExtra("userAddress");
		if (object != null) {
			userAddress = (UserAddress) object;
			et_address.setText(userAddress.getAddress());
			et_code.setText(userAddress.getArea());
			et_name.setText(userAddress.getName());
			et_tel.setText(userAddress.getTelephone());
			addressId = userAddress.getId();
		}
		rQueue = MyVolley.getRequestQueue();

	}

	/**
	 * 初始化
	 */
	public void init() {
		btn_delete = (Button) findViewById(R.id.btn_delete);
		btn_delete.setOnClickListener(btn_deleteLisener);
		et_address = (EditText) findViewById(R.id.et_address);
		et_name = (EditText) findViewById(R.id.et_name);
		et_code = (EditText) findViewById(R.id.et_code);
		et_tel = (EditText) findViewById(R.id.et_tel);
		et_code.setFocusable(false);
		et_code.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CityDialog dialog = new CityDialog(AddressEditActivity.this);
				dialog.setDialogCallback(dialogcallback);
				dialog.show();
			}
		});
		btn_save = (Button) findViewById(R.id.btn_save);
		btn_save.setOnClickListener(save_ClickListener);

		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
	}

	Dialogcallback dialogcallback = new Dialogcallback() {
		@Override
		public void dialogdo(String province, String city, String couny,
				String zip) {
			// if (zip != null) {
			// et_code.setTag(zip);
			// }
			if (province.contains("市")) {
				et_code.setText(province + couny);
			} else {
				et_code.setText(province + city + couny);
			}
		}
	};
	OnClickListener save_ClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			String address = et_address.getText().toString();
			String name = et_name.getText().toString();
			String tel = et_tel.getText().toString();
			String area = et_code.getText().toString();
//			if (et_code.getTag().toString() != null) {
//				code = et_code.getTag().toString();
//			}
			try {
				address = URLEncoder.encode(address, "UTF-8");
				name = URLEncoder.encode(name, "UTF-8");
				tel = URLEncoder.encode(tel, "UTF-8");
				area = URLEncoder.encode(area, "UTF-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			String url = VMConst.REQUEST_URL + "changeAddress?addressId="
					+ addressId + "&address=" + address + "&zip=" + code
					+ "&name=" + name + "&telephone=" + tel + "&area=" + area;

			StringRequest addAddressRequest = new StringRequest(url,
					new Listener<String>() {

						@Override
						public void onResponse(String arg0) {
							Intent intent = new Intent(
									AddressEditActivity.this,
									AddressManageActivity.class);
							intent.putExtra("order", order);
							startActivity(intent);
							finish();
						}
					}, new ErrorListener() {

						@Override
						public void onErrorResponse(VolleyError arg0) {
							Toast toast = Toast.makeText(
									AddressEditActivity.this, "修改地址失败",
									Toast.LENGTH_SHORT);
							toast.show();
						}

					});

			rQueue.add(addAddressRequest);

		}
	};
	OnClickListener btn_deleteLisener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String url = VMConst.REQUEST_URL + "deleteAddress?addressId="
					+ addressId;
			StringRequest deleteAddressRequest = new StringRequest(url,
					new Listener<String>() {

						@Override
						public void onResponse(String arg0) {
							// TODO Auto-generated method stub
							Toast toast = Toast.makeText(
									AddressEditActivity.this, "删除地址成功",
									Toast.LENGTH_SHORT);
							toast.show();
							Intent intent = new Intent(
									AddressEditActivity.this,
									AddressManageActivity.class);
							intent.putExtra("order", order);
							startActivity(intent);
							finish();
						}

					}, new ErrorListener() {

						@Override
						public void onErrorResponse(VolleyError arg0) {
							Toast toast = Toast.makeText(
									AddressEditActivity.this, "删除地址失败",
									Toast.LENGTH_SHORT);
							toast.show();
						}
					});
			rQueue.add(deleteAddressRequest);
		}
	};
	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();

	}

}
